using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Acms.AcmsControls.ContentControls.FrontEnd;
using Acms.AcmsControls;
using System.Text;
using Acms.Bll;

public partial class Master : BaseMaster
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            StringBuilder flashLinkUrl = new StringBuilder();
            flashLinkUrl.Append("http://");
            flashLinkUrl.Append(Request.ServerVariables["Server_Name"]);
            flashLinkUrl.Append(ResolveUrl("~/WorldClass/default.aspx?Validated=true"));
            flashLink.Variables.Add("LinkUrl", flashLinkUrl.ToString());

            StringBuilder flashUrl = new StringBuilder();
            flashUrl.Append("http://");
            flashUrl.Append(Request.ServerVariables["Server_Name"]);
            flashUrl.Append(ResolveUrl("~/ImagesXml.aspx"));
            string spacer = "?";
            if(AcmsPage.AcmsContext.SiteVersion == null)
            {
                flashUrl.Append("?AcmsSV=Pending");
                spacer = "&";
            }
            else if(AcmsPage.AcmsContext.SiteVersion.Value > DateTime.Now)
            {
                flashUrl.Append("?AcmsSV=");
                flashUrl.Append(Server.UrlEncode(AcmsPage.AcmsContext.SiteVersion.Value.ToString("yyyy/MM/dd HH:mm")));
                spacer = "&";
            }
            flashUrl.Append(spacer);
            flashUrl.Append("AcmsControlName=Image Content");

            //flashSlideShow.Variables.Add("resolveUrl", flashUrl.ToString());
        }
        //btnSearch.PostBackUrl = "~/Search.aspx";
        //Page.ClientScript.RegisterStartupScript(this.GetType(), "MenuStartup", string.Format("startList(\"{0}\");", menuIssues.ClientID), true);
    }

    private TemplatePage AcmsPage
    {
        get
        {
            return Page as TemplatePage;
        }
    }

    protected void btnSearch_Click(object sender, ImageClickEventArgs e)
    {
        if (Page.IsValid)
        {
            StringBuilder url = new StringBuilder();
            url.Append("~/Search.aspx");
            string spacer = "?";
            foreach (string key in Request.QueryString.AllKeys)
            {
                if (key != null && key.ToLower() != "text")
                {
                    url.Append(spacer);
                    url.Append(key);
                    url.Append("=");
                    url.Append(Request.QueryString[key]);
                    spacer = "&";
                }
            }
            url.Append(spacer);
            url.AppendFormat("Text={0}", txtSearch.Text.Trim());

            Response.Redirect(Page.ResolveUrl(url.ToString()));
        }
    }

    protected void dsTopics_SelectData(object sender, Acms.DataControls.SelectEventArgs<Acms.Bll.Topic> e)
    {
        e.SetData(Acms.Bll.Topic.GetAllBySiteIdAndTopicGroupPath(((TemplatePage)Page).AcmsContext.SiteId, "ReserveSociety"));
    }

    public override void SetTopicTitle(string topicName)
    {
        lblTopicName.Text = topicName;
    }

    public override ImageButton SearchButton
    {
        get { return btnSearch; }
    }

    public override TextBox SearchTextBox
    {
        get { return txtSearch; }
    }

    public override void SetTopic(Acms.Bll.Topic topic)
    {
        Image topicImageBackUp = upTopicImage.FindControl("topicImageBackUp") as Image;
        AcmsImage topicImage = upTopicImage.FindControl("topicImage") as AcmsImage;
        if (topicImageBackUp != null && topicImage != null)
        {
            Acms.Bll.TopicFile file = topic.GetAttachment("HeaderImage");
            if (file != null)
            {
                topicImageBackUp.Visible = false;
                topicImage.Visible = true;
                topicImage.FileId = file.FileId;
                topicImage.ToolTip = topic.Name;
            }
            else
            {
                topicImageBackUp.Visible = true;
                topicImage.Visible = false;
            }
        }
        upTopicImage.Update();
    }

    protected void rptTopics_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Acms.Bll.Topic topic = e.Item.DataItem as Acms.Bll.Topic;
            HyperLink link = e.Item.FindControl("lnkTopic") as HyperLink;
            AcmsImage topicImage = upTopicImage.FindControl("topicImage") as AcmsImage;
            Image topicImageBackUp = upTopicImage.FindControl("topicImageBackUp") as Image;

            if (link != null)
            {
                link.NavigateUrl = string.Format("{1}#Topic_{0}", topic.Id, Page.Request.RawUrl);
                link.Text = topic.Name;
                link.Attributes.Add("onclick", string.Format("javascript:return topicBarClick(this, {0}, \"{1}\", \"{2}\")", topic.Id.ToString(), topicImage.ClientID, topicImageBackUp.ClientID));
                Acms.Bll.TopicFile file = topic.GetAttachment("HeaderImage");
                if (file != null)
                {
                    Page.ClientScript.RegisterExpandoAttribute(link.ClientID, "TopicImage", Page.ResolveClientUrl(string.Format("~/File.aspx?Id={0}", file.FileId)));
                }
                else
                {
                    Page.ClientScript.RegisterExpandoAttribute(link.ClientID, "TopicImage", Page.ResolveClientUrl("~/img/Classic_cocktail.jpg"));
                }
            }
        }
    }
    protected void rptTopics_ItemCreated(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            HtmlGenericControl li = new HtmlGenericControl("li");
            HyperLink link = new HyperLink();
            link.ID = "lnkTopic";
            li.Controls.Add(link);
            e.Item.Controls.Add(li);
        }
    }

    public override void FixTopicImage(string path)
    {
        topicImageBackUp.ImageUrl = path;
        topicImageBackUp.Visible = true;
        topicImage.Visible = false;
    }
}
